package com.yitao.cms.service.procurement;


import com.skipper.base.service.GenericEntityService;
import com.yitao.client.dto.purchase.ProcurementOrderDetailDto;
import com.yitao.client.model.purchase.ProcurementOrderDetailModel;
import com.yitao.client.model.purchase.QProcurementOrderDetailModel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.util.List;

@Service
public class CmsProcurementOrderDetailService extends GenericEntityService<ProcurementOrderDetailModel, ProcurementOrderDetailDto, QProcurementOrderDetailModel> {


    public CmsProcurementOrderDetailService(EntityManager entityManager) {
        super(entityManager);
    }

    @Transactional
    public void save(ProcurementOrderDetailModel procurementOrderDetailModel) {
        repository.save(procurementOrderDetailModel);
    }

    /**
     * 根据ID查询详细信息
     *
     * @param id
     * @return
     */
    public ProcurementOrderDetailModel getProcurementOrderDetailModel(Long id) {
        return queryFactory.select(Q).from(Q).where(Q.id.eq(id)).fetchOne();
    }

    /**
     * 查询缺货信息
     *
     * @param orderDetailId
     * @return
     */
    public ProcurementOrderDetailModel getStockoutInfoByOrderDetailId(Long orderDetailId) {
        List<ProcurementOrderDetailModel> fetch = queryFactory.select(Q).from(Q).where(Q.orderDetailId.eq(orderDetailId).and(Q.procurementStatus.eq(4))).fetch();
        if(fetch != null && fetch.size() >0) {
            return fetch.get(0);
        }
        return null;
    }

    /**
     * 查询缺货列表
     *
     * @return
     */
    public List<ProcurementOrderDetailModel> getProcurementOrderDetailInfoByOrderDetailIdAndStockout(Long orderDetailInfoId) {
        return queryFactory.select(Q).from(Q).where(Q.orderDetailId.eq(orderDetailInfoId).and(Q.procurementStatus.eq(4))).fetch();
    }
}
